<template>
  <div>
    <common-head title="确认订单"/>
    <div class="wrap">
      <van-contact-card
        :type="cardType"
        :name="contact.linkMan"
        :tel="contact.mobile"
        @click="onEdit"
      />
      <div class="items">
        <div class="item" v-for="item in items" :key="item.id">
            <van-card
              :num="item.number"
              :price="item.price"
              :title="item.name"
              :thumb="item.pic"
              />
        </div>
      </div>
    </div>
    <van-submit-bar :price="total*100" button-text="提交订单" @submit="onSubmit" />
  </div>
</template>
<script>
import CommonHead from '../../components/CommonHead.vue'
import { fetchDefaultContact, fetchContactInfo, getCartInfo } from '@api'
const token = localStorage.getItem('token')
export default {
  data () {
    return {
      // 订单联系人
      contact: {},
      // 订单商品
      items: [],
      // 总价格
      total: 0
    }
  },
  created () {
    this.fetchItems()
    this.fetchContact()
  },
  methods: {
    onSubmit () {
      alert('到支付页面')
    },
    onEdit () {
      // 修改或者添加联系人
      this.$router.push('/contactLists')
    },
    fetchContact () {
      // 获取订单联系人
      if (this.$route.query.id) {
        fetchContactInfo({
          id: this.$route.query.id,
          token
        }).then(res => {
          console.log(res)
          if (res.data.code === 0) {
            this.contact = res.data.data.info
          }
        })
      } else {
        fetchDefaultContact({
          token
        }).then(res => {
          console.log(res)
          if (res.data.code === 0) {
            this.contact = res.data.data.info
          }
        })
      }
    },
    fetchItems () {
      // 获取订单商品
      getCartInfo({
        token
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.items.filter(item => item.selected)
          this.total = res.data.data.price
        }
      })
    }
  },
  computed: {
    cardType () {
      return this.contact.id ? 'edit' : 'add'
    }
  },
  components: { CommonHead }
}
</script>

<style lang="scss" scoped>
.wrap{
  padding-top: 46px;
  padding-bottom: 50px;
}
</style>
